/*
 * Famon (http://code.google.com/p/famon/)
 * Copyright (c) 2010 Carl Crous.
 *
 * This file is part of Famon.
 *
 * Famon is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Famon is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Famon.  If not, see <http://www.gnu.org/licenses/>.
 */

package za.org.foobar.famon.frontend.server.data;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.gwtportlets.portlet.smartgwt.client.CriteriaDto;
import org.gwtportlets.portlet.smartgwt.client.DataTransferObject;
import org.gwtportlets.portlet.smartgwt.client.SmartPortletFactory;
import org.gwtportlets.portlet.smartgwt.server.DataProviderObject;
import org.gwtportlets.portlet.smartgwt.server.SmartFilter;
import za.org.foobar.famon.frontend.client.dto.ApplicationGroupDto;
import za.org.foobar.famon.frontend.server.ApplicationLogic;
import za.org.foobar.famon.frontend.server.FamonSecurityException;
import za.org.foobar.famon.model.Application;
import za.org.foobar.famon.model.ApplicationInstance;
import za.org.foobar.famon.model.ApplicationVersion;

import java.util.List;

/**
 *
 */
public class ApplicationGroupDpo implements DataProviderObject {
    private final Log log = LogFactory.getLog(getClass());
    private static ApplicationGroupDpo instance;

    public static ApplicationGroupDpo get() {
        return instance != null ? instance : (instance = new ApplicationGroupDpo());
    }

    public String getDataSourceId() {
        return ApplicationGroupDto.DATA_SOURCE_ID;
    }

    public List<DataTransferObject> fetchData(SmartPortletFactory f) {
        ApplicationLogic logic = ApplicationLogic.get();
        CriteriaDto criteria = f.getCriteria();
        Application application = null;
        ApplicationInstance applicationInstance = null;
        ApplicationVersion applicationVersion = null;
        if (criteria != null) {
            String applicationName = SmartFilter.getSimpleCriteriaValue(criteria, "applicationName");
            if (applicationName != null) {
                try {
                    application = logic.getApplicationByName(applicationName);
                } catch (FamonSecurityException ignored) {
                }
                if (application == null) {
                    log.error("Application not found \"" + applicationName + "\"");
                }
            }
            String instanceName = SmartFilter.getSimpleCriteriaValue(criteria, "instanceName");
            if (application != null && instanceName != null) {
                applicationInstance = logic.getApplicationInstanceByName(application, instanceName);
                if (applicationInstance == null) {
                    log.error("Application Instance not found \"" + instanceName + "\"");
                }
            }
            String versionName = SmartFilter.getSimpleCriteriaValue(criteria, "versionName");
            if (application != null && versionName != null) {
                applicationVersion = logic.getApplicationVersionByName(application, versionName);
                if (applicationVersion == null) {
                    log.error("Application Version not found \"" + versionName + "\"");
                }
            }
        }
        return f.limitList(logic.getApplicationGroupDtoList(application != null ? application.getId() : null,
                applicationInstance != null ? applicationInstance.getId() : null,
                applicationVersion != null ? applicationVersion.getId() : null));
    }

    public DataTransferObject updateData(SmartPortletFactory smartPortletFactory) {
        return null;
    }

    public DataTransferObject addData(SmartPortletFactory smartPortletFactory) {
        return null;
    }

    public DataTransferObject removeData(SmartPortletFactory smartPortletFactory) {
        return null;
    }
}
